package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.api.*;
import com.tanhua.dubbo.pojo.*;
import com.tanhua.server.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SoulTestService {
    @DubboReference(version = "1.0.0")
    private SoulQuestionApi soulQuestionApi;

    @DubboReference(version = "1.0.0")
    private QuestionnaireApi questionnaireApi;

    @DubboReference(version = "1.0.0")
    private OptionApi optionApi;

    @DubboReference(version = "1.0.0")
    private ReportApi reportApi;

    @DubboReference(version = "1.0.0")
    private UserReportApi userReportApi;

    @DubboReference(version = "1.0.0")
    private DimensionsApi dimensionsApi;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    /**
     * 查询测试列表
     *
     * @return
     */
    public List<QuestionnaireVo> queryQuestionnaireList() {
        Long userId = UserThreadLocal.get();
        String[] levels = new String[]{"初级", "中级", "高级"};
        String level = "";
        List<QuestionnaireVo> questionnaireVoList = new ArrayList<>();
        //List<QuestionnaireVo> questionnaireVoList = new ArrayList<>();
        for (int i = 0; i < levels.length; i++) {
            level = levels[i];
            Questionnaire questionnaire = this.questionnaireApi.queryQuestionnaireByLevel(level);
            List<SoulQuestion> soulQuestionList = this.soulQuestionApi.queryByLevel(level);
            //soulQuestionList 转换为soulQuestionVoList
            List<SoulQuestionVo> soulQuestionVoList = new ArrayList<>();
            for (SoulQuestion soulQuestion : soulQuestionList) {
                SoulQuestionVo soulQuestionVo = BeanUtil.toBeanIgnoreError(soulQuestion, SoulQuestionVo.class);
                soulQuestionVo.setId(soulQuestion.getId().toString());
                //设置问题选项
                List<Option> options = this.optionApi.queryBySoulQuestionId(soulQuestion.getId());
                List<OptionVo> optionVoList = new ArrayList<>();
                for (Option option : options) {
                    OptionVo optionVo = new OptionVo();
                    optionVo.setId(option.getId().toString());
                    optionVo.setOption(option.getOptionText());
                    optionVoList.add(optionVo);
                }
                soulQuestionVo.setOptions(optionVoList);
                soulQuestionVoList.add(soulQuestionVo);
            }
            QuestionnaireVo questionnaireVo = BeanUtil.toBeanIgnoreError(questionnaire, QuestionnaireVo.class);
            questionnaireVo.setQuestions(soulQuestionVoList);
            if (i >= 1) {
                UserReport userReport = this.userReportApi.queryByUserIdLevel(userId, levels[i-1]);
                questionnaireVo.setIsLock(null == userReport ? 1 : 0);
            }else{
                questionnaireVo.setIsLock(0);
            }
            /*if (level.equals("高级")) {
                questionnaireVo.setReportId("1");
                questionnaireVo.setIsLock(1);
            }*/

            questionnaireVoList.add(questionnaireVo);
        }
        return questionnaireVoList;
    }

    /**
     * @param answersList
     * @return
     */
    public Object answers(AnswersList answersList) {
        Long userId = UserThreadLocal.get();
        Double finalScore = 0.00;
        Long questionnaireId = 0L;
        String level = "";
        //获取到最终总分数
        List<Answers> answers = answersList.getAnswers();
        for (Answers answer : answers) {
            Option option = option = this.optionApi.queryByOptionId(Convert.toLong(answer.getOptionId()));
            finalScore += option.getScore();
            SoulQuestion soulQuestion = this.soulQuestionApi.queryBySoulQuestionId(Convert.toLong(answer.getQuestionId()));
            level = soulQuestion.getLevel();
            Questionnaire questionnaire = this.questionnaireApi.queryQuestionnaireByLevel(level);
            questionnaireId = questionnaire.getId();
        }
        List<Report> reportList = this.reportApi.queryReportList(questionnaireId);
        Report report = new Report();
        for (Report report1 : reportList) {
            if (finalScore > report1.getMinScore() && finalScore < report1.getMaxScore()) {
                report = report1;
                break;
            }
        }
        UserReport userReport = new UserReport();
        userReport.setUserId(UserThreadLocal.get());
        userReport.setQuestionnaireId(questionnaireId);
        userReport.setReportId(report.getId());
        userReport.setScore(finalScore);
        userReport.setLevel(level);
        UserReport userReportBefore = this.userReportApi.queryByUserIdLevel(userId, level);
        if (null != userReportBefore) {
            int i = this.userReportApi.updateReport(userReport, UserThreadLocal.get());

        } else {
            Integer insert = this.userReportApi.insert(userReport);
        }

        return report.getId().toString();
    }

    /**
     *
     * @param reportId
     * @return
     */
    public Object getReport(Long reportId) {
        Long userId = UserThreadLocal.get();
        Report report = this.reportApi.queryByReportId(reportId);
        Dimensions dimensionsResult = this.dimensionsApi.queryByReportId(reportId);
        ReportVo reportVo = new ReportVo();
        reportVo.setConclusion(report.getConclusion());
        reportVo.setCover(report.getCover());
        //添加维度
        List<DimensionVo> dimensionVoList = new ArrayList<>();
        DimensionVo dimensionVo1 = new DimensionVo("内向", dimensionsResult.getNeixiang());
        dimensionVoList.add(dimensionVo1);
        DimensionVo dimensionVo2 = new DimensionVo("理性", dimensionsResult.getNeixiang());
        dimensionVo1.setValue(dimensionsResult.getLixing());
        dimensionVoList.add(dimensionVo2);
        DimensionVo dimensionVo3 = new DimensionVo("判断", dimensionsResult.getNeixiang());
        dimensionVo3.setValue(dimensionsResult.getPanduan());
        dimensionVoList.add(dimensionVo3);
        DimensionVo dimensionVo4 = new DimensionVo("可靠", dimensionsResult.getNeixiang());
        dimensionVo4.setValue(dimensionsResult.getKekao());
        dimensionVoList.add(dimensionVo4);
        reportVo.setDimensions(dimensionVoList);
        //添加相似用户信息
        Long questionnaireId = report.getQuestionnaireId();
        List<UserReport> userReportList = this.userReportApi.querySimilarList(reportId,questionnaireId,userId);
        List<UserReport> userReports = CollUtil.sortByProperty(userReportList, "score");
        List<SimilarUserInfo> similarYou = new ArrayList<>();
        for (UserReport userReport1 : userReports) {
            SimilarUserInfo similarUserInfo = new SimilarUserInfo();
            Long userId1 = userReport1.getUserId();
            similarUserInfo.setId(userId1.intValue());
            similarUserInfo.setAvatar(this.userInfoApi.queryByUserId(userId1).getLogo());
            similarYou.add(similarUserInfo);
        }
        //设置相似用户
        reportVo.setSimilarYou(similarYou);
        return reportVo;
    }
}
